package com.example.firstapp;

import android.os.Bundle;
import android.view.MenuItem;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import com.example.firstapp.fragment.AddressFragment;
import com.example.firstapp.fragment.DiscoveryFragment;
import com.example.firstapp.fragment.HomeFragment;
import com.example.firstapp.fragment.MeFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import java.util.ArrayList;
import java.util.List;

public class WxActivity extends AppCompatActivity {

    BottomNavigationView bottomNavigation;
    List<Fragment> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_wx);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        bottomNavigation = findViewById(R.id.bottomNavigation);
        list = new ArrayList<>();
        list.add(new HomeFragment());
        list.add(new AddressFragment());
        list.add(new DiscoveryFragment());
        list.add(new MeFragment());

        showFragment(list.get(0));

        // 底部导航栏切换事件
        bottomNavigation.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//                Log.i("===myLog===","导航栏目信息: "+ item.getTitle());
                switch (item.getOrder()){
                    case 100:
                        showFragment(list.get(0));
                        break;
                    case 200:
                        showFragment(list.get(1));
                        break;
                    case 300:
                        showFragment(list.get(2));
                        break;
                    case 400:
                        showFragment(list.get(3));
                        break;
                }
                return true;
            }
        });
    }

    private void showFragment(Fragment fragment) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.container, fragment);
        ft.commit();
    }
}